<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="区块链," />










<meta name="description" content="前言最近看了李永乐老师的比特币课程，做了些笔记，也让我了解了比特币大概是个什么东西，下面主要以比特币是什么、他存在的问题、和特点三个方面讲述下比特币的原理。 脑图链接：点此获取脑图  比特币是什么？比特币是一种数字货币，是一种基于密码学的货币，在2008年的金融危机时候，出现了一个网络极客化名为中本聪发表了一片论文《一种点对点的电子现金系统》。 去中心化电子记账系统点对点的电子现金系统，实际上就一">
<meta property="og:type" content="article">
<meta property="og:title" content="比特币原理">
<meta property="og:url" content="http://yoursite.com/p/581d1ebe.html">
<meta property="og:site_name" content="Molzhao">
<meta property="og:description" content="前言最近看了李永乐老师的比特币课程，做了些笔记，也让我了解了比特币大概是个什么东西，下面主要以比特币是什么、他存在的问题、和特点三个方面讲述下比特币的原理。 脑图链接：点此获取脑图  比特币是什么？比特币是一种数字货币，是一种基于密码学的货币，在2008年的金融危机时候，出现了一个网络极客化名为中本聪发表了一片论文《一种点对点的电子现金系统》。 去中心化电子记账系统点对点的电子现金系统，实际上就一">
<meta property="og:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%A1%88%E4%BE%8B2.png">
<meta property="og:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%A1%88%E4%BE%8B1.png">
<meta property="og:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E5%8C%BA%E5%9D%97%E7%BB%84%E6%88%90.png">
<meta property="og:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E9%9D%9E%E5%AF%B9%E7%A7%B0%E5%8A%A0%E5%AF%86.png">
<meta property="og:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E4%BD%99%E9%A2%9D%E6%A3%80%E6%9F%A5.png">
<meta property="og:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E5%8F%8C%E9%87%8D%E6%94%AF%E4%BB%98%E9%97%AE%E9%A2%98.png">
<meta property="og:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%9C%80%E9%95%BF%E9%93%BE%E5%8E%9F%E5%88%99.png">
<meta property="article:published_time" content="2021-05-23T13:54:00.000Z">
<meta property="article:modified_time" content="2022-05-30T02:09:24.504Z">
<meta property="article:author" content="MolZhao">
<meta property="article:tag" content="区块链">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%A1%88%E4%BE%8B2.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"我们没有找到任何搜索结果: ${query}","hits_stats":"找到约${hits}条结果 (用时 ${time}ms)"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/p/581d1ebe.html"/>





  <title>比特币原理 | Molzhao</title>
  








<meta name="generator" content="Hexo 4.2.1"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Molzhao</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>

                

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/p/581d1ebe.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="MolZhao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Molzhao">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">比特币原理</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-05-23T21:54:00+08:00">
                2021-05-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%9A%8F%E7%AC%94/index.html" itemprop="url" rel="index">
                    <span itemprop="name">随笔</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/p/581d1ebe.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/p/581d1ebe.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i> 阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.7k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>最近看了<a href="https://www.bilibili.com/video/BV1my4y1b71H?p=2&t=952" target="_blank" rel="noopener">李永乐老师的比特币课程</a>，做了些笔记，也让我了解了比特币大概是个什么东西，下面主要以比特币是什么、他存在的问题、和特点三个方面讲述下比特币的原理。</p>
<p><strong><em>脑图链接：</em></strong><a href="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%AF%94%E7%89%B9%E5%B8%81.xmind" target="_blank" rel="noopener">点此获取脑图</a></p>
<hr>
<h2 id="比特币是什么？"><a href="#比特币是什么？" class="headerlink" title="比特币是什么？"></a>比特币是什么？</h2><p>比特币是一种数字货币，是一种基于密码学的货币，在2008年的金融危机时候，出现了一个网络极客化名为中本聪发表了一片论文《一种点对点的电子现金系统》。</p>
<h3 id="去中心化电子记账系统"><a href="#去中心化电子记账系统" class="headerlink" title="去中心化电子记账系统"></a>去中心化电子记账系统</h3><p>点对点的电子现金系统，实际上就一个去中心化的电子记账系统。我们在交易的时候，最重要的就是记账，比如说我付给你多少多少钱，检查银行卡里还有多少钱，现在这个记账谁来记呢，银行来记，不管信用卡还是支付宝还是微信都是银行记账，我们信任银行，银行代表着国家的信用。</p>
<p>中本聪就说我们不需要一个中心化的记账系统，我们可以去中心化，每个人的账本信息都是公开的，这就叫去中心化电子记账系统。</p>
<h3 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h3><p>举个例子有ABCD四个小朋友用比特币进行交易。A支付B十个比特币，他需要把交易信息广播给所有人。<br><img src="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%A1%88%E4%BE%8B2.png" alt="交易1"></p>
<p>同理B支付C十个比特币也需要把交易信息广播给其他人。<br><img src="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%A1%88%E4%BE%8B1.png" alt="交易2"></p>
<p>上面这两个账单信息，我们会打包成一个块，这个块我们称之为区块，一个区块大概是1MB，可以存4000条左右的交易记录，打包好之后我们会将这个块接到前面的块上，形成一条链，就叫<font color="red">区块链。</font></p>
<hr>
<h2 id="存在的问题"><a href="#存在的问题" class="headerlink" title="存在的问题"></a>存在的问题</h2><h3 id="为什么要记账"><a href="#为什么要记账" class="headerlink" title="为什么要记账"></a>为什么要记账</h3><p>凭啥花我们的电脑资源要记录和自己无关的账单信息。是因为比特币有个规则，参与记账会获得相应的奖励。</p>
<h4 id="手续费"><a href="#手续费" class="headerlink" title="手续费"></a>手续费</h4><p>手续费，A-&gt;B需要付一定的手续费给这个打包的人，要是用银行卡的话我们还是需要手续费，而且手续费挺高的，要是使用比特币的话，手续费就低了</p>
<h4 id="打包奖励"><a href="#打包奖励" class="headerlink" title="打包奖励"></a>打包奖励</h4><p>打包奖励，只能是一个人打包，一个人会获得奖励，奖励是多少呢，中本聪在2008年提出这个系统的时候，设计了这么一个方案，每十分钟打一个包，最开始的包会奖励打包者50个比特币，过了四年后会奖励打包者25个比特币，没过四年减半。</p>
<p>一个小时有六个十分钟，每个十分钟50个比特币，一天24小时，一年365天，没过四年减半，大概是2100W个比特币。<br><code>50*6*24*365*4*（1+1/2+1/4+...）= 2100W</code></p>
<h3 id="账单以谁的为准"><a href="#账单以谁的为准" class="headerlink" title="账单以谁的为准"></a>账单以谁的为准</h3><p>既然打包可以获得这么多奖励，每个人都会打包账单，人人都抢着打包，那以谁都包为准呢，就是通过工作量证明来验证，这个就是做一个很难都数学题，难到没有任何一个人能通过脑子给他做出来，只能通过穷举法，只要你把他尝试出来了，就是获得了奖励，这个过程称之为挖矿成功了。</p>
<p>首先我们要先了解下<strong><em>哈希算法：</em></strong>正着算很容易，反着算很难。传入一个字符串不管都长最后都会生成256位二进制数，就算传个电影进去也是一样。</p>
<h4 id="区块的组成"><a href="#区块的组成" class="headerlink" title="区块的组成"></a>区块的组成</h4><p>区块链就是一连串的交易信息，这个块里不只是块里的信息，有个头部有个信息体，信息体里就是账单，但是每个人账单都不一样，他们都想在区块链上接快，然后就要算一个很难的数学题。</p>
<p><img src="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E5%8C%BA%E5%9D%97%E7%BB%84%E6%88%90.png" alt="区块组成"></p>
<p>如上图现在有很多人都想去打包，因为打包有奖励，还有手续费，每个人都有自己都账单，这个账单是收集了周围一大堆人的账单，每个人的拿着自己的账单准备去接块。一般一个区块要包含如下的信息：</p>
<ol>
<li>字符串：前块头部+账单+时间戳+随机数</li>
<li>对字符串两次hash运算，hash=sha256(sha256(字符串))</li>
<li>要求前n位必须是0:000000000，比如说有9个零，只要前面是9个零就算对了，你就有资格去打包，有资格去打包的意思就是说你要吧你这个hash值，作为一个新块的头部打一个包，打完之后再把这个包接到区块链后面去，接出一个新块就获得了想要的奖励，如何算出9个0呢就是要改变上述的随机数，因为其他内容是改不了的，只能改变随机数，每个人计算时候难度不一样，虽然前块的头部一样，但是每个人打包的账单是不一样的，时间也是不一样的，个人信息也是不一样的，造成随机数大小不同，有人运气好可能第一个就算出来了，运气不好，到死都算不出来，但是平均来说是谁都计算能力越强，谁更有希望打包这个块，挖到这个矿，所以为什么批量去买矿机。</li>
</ol>
<h4 id="区块产生频率确定"><a href="#区块产生频率确定" class="headerlink" title="区块产生频率确定"></a>区块产生频率确定</h4><p><strong><em>难度N的确定：</em></strong>上面的n的个数越多就越难，为什么呢？第一位是0概率是1/2，第二位也是1/2于是就是1/2*1/2…..最后就是1/2的n次幂，n越大越难，当时设计的时候保证每十分钟，需要出一个块，打包几千条信息</p>
<p><strong><em>难度N的调整：</em></strong>如何保证十分钟出一个块就是调整n的系数？例如有1W台矿机，每台计算能力是14T/S=1.4<em>10的十三次方每秒。十分钟大概有8</em>10的19次方次运算， 这个大概是2的66次幂。这就情况下矿机就会把难度设置为66，第一个能算出来前66位都是0的人就能得到打包奖励，你不能保证你运气好，你只能做的就是拼命去买矿机去挖矿，这样就能更高概率获得比特币。</p>
<h3 id="如何防伪"><a href="#如何防伪" class="headerlink" title="如何防伪"></a>如何防伪</h3><p>比特币的账单信息都是公开且透明的，比如B广播了一条消息，A支付了10个币给B，实际上A并没有支付，就是一条伪造的信息，我们怎么去防伪，怎么防止篡改。</p>
<p><strong><em>身份如何进行认证：</em></strong></p>
<ol>
<li>传统方式：人脸识别、指纹、签名显然在电子支付系统上都很好伪造</li>
<li>电子签名系统，每个比特币用户在注册账户时都会生成一个随机数，除了随机数还有私钥公钥和一个地址，如果想让别人给你钱只要给地址就好了，如果你给别人钱就要给公钥和地址信息即可。</li>
</ol>
<p>这边需要引入一个概念<font color="red">非对称加密</font>,只有你可以加密，任何人都可以解密你加密之后的信息，例如：A付给B十个比特币，把上面这条交易信息加密成一个摘要信息，再用私钥进行加密成密文,公钥加密码解密成一个摘要，只要最后两个摘要不一样，就所有人认为这条消息是伪造的.<br><img src="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E9%9D%9E%E5%AF%B9%E7%A7%B0%E5%8A%A0%E5%AF%86.png" alt="非对称机密"></p>
<h3 id="如何防止双重支付"><a href="#如何防止双重支付" class="headerlink" title="如何防止双重支付"></a>如何防止双重支付</h3><p>场景：如果A给B十个比特币，但是A没有十个比特币怎么办，或者说A本来就十个比特币，但是同时发了两条消息，分别给BC两人各十个比特币，这种情况如何避免，如何解决双重支付问题？</p>
<h4 id="余额检查"><a href="#余额检查" class="headerlink" title="余额检查"></a>余额检查</h4><p>我们知道区块链是把很多交易信息，一块一块打包并链起来，每个人使用区块链时，都会把整个链下下来，包括最开始创世纪的块。</p>
<p>比如A有50个比特币，他同时发出两条交易信息，一条是A付给B十个比特币，第二条是A付给B六十个比特币，接受到消息的人都会去检查A的余额，发现A有十个比特币，则这条信息就被其他人所接受，如果发现A没有六十个比特币，这条信息就会被周围所有人拒绝。<br><img src="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E4%BD%99%E9%A2%9D%E6%A3%80%E6%9F%A5.png" alt="余额检查"></p>
<h4 id="双重支付"><a href="#双重支付" class="headerlink" title="双重支付"></a>双重支付</h4><p>现实生活中卖房子，有人卖房子同时和两个买家签约这种就叫做双重支付。</p>
<p><img src="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E5%8F%8C%E9%87%8D%E6%94%AF%E4%BB%98%E9%97%AE%E9%A2%98.png" alt="双重支付"></p>
<h3 id="如何防止篡改"><a href="#如何防止篡改" class="headerlink" title="如何防止篡改"></a>如何防止篡改</h3><p>比如有一个想篡改记录的人，他是不可能伪造别人的电子签名的，因为电子签名无法伪造，但他可以删掉某一条记录。比特币最长链原则，就是一个区块链能作为主链。</p>
<p><img src="https://molzhao-pic.oss-cn-beijing.aliyuncs.com/2021/2021-05-23/bitcoin/%E6%9C%80%E9%95%BF%E9%93%BE%E5%8E%9F%E5%88%99.png" alt="最长链原则"></p>
<p>如果有一个人希望抹掉自己的一条记录，他可以在区块链中间一条块重新计算重新打包，计算出一条链来，这个工作量是很大的，全世界一起算十分钟出一个，你伪造出来一个块不含你的交易信息，但是你这个块别人不承认，因为不够长，所以你在你自己这条链下面继续算继续算，什么时候长过主链了信息也就篡改成功了。</p>
<p>所以你想篡改记录是和全世界人为敌，所以大额交易是要等几个块的，等六个块之后，别人想篡改也就不容易了</p>
<hr>
<h2 id="比特币的特点"><a href="#比特币的特点" class="headerlink" title="比特币的特点"></a>比特币的特点</h2><p>1、比特币作为一种新的支付手段是非常新颖和漂亮的。<br>2、比特币又没有中心发行机构，不用担心主权危机。<br>3、比特币总量固定不会发生滥发的风险。<br>4、比特币天生具有防伪属性，而且可以追溯。<br>5、交易的时候手续费非常低。<br>6、因为比特币为匿名货币，在进行交易的时候只需要一个公钥和一个地址，不能知道真实身份，于是活跃于暗网，招到很多政府机关打击。<br>7、有些人把他当成投机手段，有人在一夜之间暴富，有人一夜之间倾家荡产。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/%E5%8C%BA%E5%9D%97%E9%93%BE/index.html" rel="tag"># 区块链</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/p/400a25e9.html" rel="next" title="Redis实现分布式锁">
                <i class="fa fa-chevron-left"></i> Redis实现分布式锁
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/p/b2642bd.html" rel="prev" title="Mybatis-Plus的一次PR经历-识别表名">
                Mybatis-Plus的一次PR经历-识别表名 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpg"
                alt="MolZhao" />
            
              <p class="site-author-name" itemprop="name">MolZhao</p>
              <p class="site-description motion-element" itemprop="description">沉淀</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/index.html">
              
                  <span class="site-state-item-count">54</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">30</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/normcorer" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:MolZhaovo@163.com" target="_blank" title="Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#比特币是什么？"><span class="nav-number">2.</span> <span class="nav-text">比特币是什么？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#去中心化电子记账系统"><span class="nav-number">2.1.</span> <span class="nav-text">去中心化电子记账系统</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#案例"><span class="nav-number">2.2.</span> <span class="nav-text">案例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#存在的问题"><span class="nav-number">3.</span> <span class="nav-text">存在的问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#为什么要记账"><span class="nav-number">3.1.</span> <span class="nav-text">为什么要记账</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#手续费"><span class="nav-number">3.1.1.</span> <span class="nav-text">手续费</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#打包奖励"><span class="nav-number">3.1.2.</span> <span class="nav-text">打包奖励</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#账单以谁的为准"><span class="nav-number">3.2.</span> <span class="nav-text">账单以谁的为准</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#区块的组成"><span class="nav-number">3.2.1.</span> <span class="nav-text">区块的组成</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#区块产生频率确定"><span class="nav-number">3.2.2.</span> <span class="nav-text">区块产生频率确定</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何防伪"><span class="nav-number">3.3.</span> <span class="nav-text">如何防伪</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何防止双重支付"><span class="nav-number">3.4.</span> <span class="nav-text">如何防止双重支付</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#余额检查"><span class="nav-number">3.4.1.</span> <span class="nav-text">余额检查</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#双重支付"><span class="nav-number">3.4.2.</span> <span class="nav-text">双重支付</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何防止篡改"><span class="nav-number">3.5.</span> <span class="nav-text">如何防止篡改</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#比特币的特点"><span class="nav-number">4.</span> <span class="nav-text">比特币的特点</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">MolZhao</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">ZYJ</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>






        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i> 访客数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i> 总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script type="text/javascript" src="/js/src/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '18BuKzLQMbUp8ECwDBiHu6Df-MdYXbMMI',
        appKey: 'xVTsu5pYLayqSvClr36TzcAH',
        placeholder: '(•̀へ•╮)',
        avatar:'identicon',
        guest_info:guest,
        pageSize:'10' || 10,
    });

    $('.vpower').html('');
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "./public/search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "default";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "default";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.5,"jsonPath":"/live2dw/assets/assets/wanko.model.json"},"display":{"superSample":2,"width":150,"height":150,"position":"right","hOffset":30,"vOffset":20},"mobile":{"show":false,"scale":0.05},"react":{"opacityDefault":0.6,"opacityOnHover":0.2},"log":false});</script></body>
</html>
